package com.ruoyi.modules.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowflakeIdUtils;
import com.ruoyi.system.util.BaiDuMapUtil;
import com.ruoyi.tagging.DisposalSiteAndProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.modules.mapper.SoilSiteInfoMapper;
import com.ruoyi.modules.domain.SoilSiteInfo;
import com.ruoyi.modules.service.ISoilSiteInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 处置场所信息Service业务层处理
 * 
 * @author ssh
 * @date 2024-12-15
 */
@Service
public class SoilSiteInfoServiceImpl implements ISoilSiteInfoService 
{
    private static final Logger log = LoggerFactory.getLogger(SoilSiteInfoServiceImpl.class);
    @Autowired
    private SoilSiteInfoMapper soilSiteInfoMapper;


    @Autowired
    private DisposalSiteAndProjectService disposalSiteAndProjectService;

    /**
     * 查询处置场所信息
     * 
     * @param id 处置场所信息主键
     * @return 处置场所信息
     */
    @Override
    public SoilSiteInfo selectSoilSiteInfoById(String id)
    {
        return soilSiteInfoMapper.selectSoilSiteInfoById(id);
    }

    /**
     * 查询处置场所信息列表
     * 
     * @param soilSiteInfo 处置场所信息
     * @return 处置场所信息
     */
    @Override
    public List<SoilSiteInfo> selectSoilSiteInfoList(SoilSiteInfo soilSiteInfo)
    {
        return soilSiteInfoMapper.selectSoilSiteInfoList(soilSiteInfo);
    }

    /**
     * 新增处置场所信息
     * 
     * @param soilSiteInfo 处置场所信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSoilSiteInfo(SoilSiteInfo soilSiteInfo)
    {
        SnowflakeIdUtils idWorker = new SnowflakeIdUtils(1, 3);
        soilSiteInfo.setId(idWorker.nextId()+"");
        soilSiteInfo.setCreateTime(DateUtils.getNowDate());

        JSONObject object = BaiDuMapUtil.geocoding(soilSiteInfo.getAddress());
        soilSiteInfo.setLatitude(object.getString("lat"));
        soilSiteInfo.setLongitude(object.getString("lng"));

        String s = disposalSiteAndProjectService.insertSoilSiteInfoBeiDouTagging(soilSiteInfo);
        //       {"rspCode":"1","poiId":"916"}
        JSONObject jsonObject = JSONObject.parseObject(s);
        soilSiteInfo.setFourSoilSiteId(jsonObject.getString("poiId"));
        soilSiteInfo.setExts(s);

//        try {
//            // todo
//            String s = disposalSiteAndProjectService.insertSoilSiteInfoBeiDouTagging(soilSiteInfo);
////            constructionSite.setFourConstructionSiteId();
//            soilSiteInfo.setExts(s);
//            // todo
////            constructionSite.setFourConstructionSiteId();
//            System.out.println("插入标注数据成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("插入标注数据失败"+e.getMessage());
//        }
        return soilSiteInfoMapper.insertSoilSiteInfo(soilSiteInfo);
    }

    /**
     * 修改处置场所信息
     * 
     * @param soilSiteInfo 处置场所信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSoilSiteInfo(SoilSiteInfo soilSiteInfo)
    {
        soilSiteInfo.setUpdateTime(DateUtils.getNowDate());

        String s = disposalSiteAndProjectService.updateSoilSiteInfoBeiDouTagging(soilSiteInfo);
        soilSiteInfo.setExts(s);
        System.out.println("处置场所修改成功"+s);
        log.info("处置场所修改成功,调用第三方接口响应参数:{}",s);
//        // todo 调用北斗接口
//        try {
//            String s = disposalSiteAndProjectService.updateSoilSiteInfoBeiDouTagging(soilSiteInfo);
//            soilSiteInfo.setExts(s);
//            System.out.println("处置场所修改成功"+s);
//            log.info("处置场所修改成功,调用第三方接口响应参数:{}",s);
//        } catch (Exception e) {
//            log.error("处置场所修改失败,异常:{}",e.getMessage());
//            throw new RuntimeException(e);
//        }
        return soilSiteInfoMapper.updateSoilSiteInfo(soilSiteInfo);
    }

    /**
     * 批量删除处置场所信息
     * 
     * @param ids 需要删除的处置场所信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSoilSiteInfoByIds(String[] ids)
    {
        for (String id : ids) {
            SoilSiteInfo siteInfo = soilSiteInfoMapper.selectSoilSiteInfoById(id);
            String s = disposalSiteAndProjectService.delSoilSiteInfoBeiDouTagging(siteInfo.getFourSoilSiteId());
            log.info("处置场所调用北斗删除标注接口成功，响应数据：{}",s);
        }
//        // todo  调用第三方接口
//        try {
//            for (String id : ids) {
//                SoilSiteInfo siteInfo = soilSiteInfoMapper.selectSoilSiteInfoById(id);
//                String s = disposalSiteAndProjectService.delSoilSiteInfoBeiDouTagging(siteInfo.getFourSoilSiteId());
//                log.info("处置场所调用北斗删除标注接口成功，响应数据：{}",s);
//            }
//
//        } catch (Exception e) {
//            log.error("处置场所调用北斗删除标注接口失败，失败原因：{}",e.getMessage());
//            throw new RuntimeException(e);
//        }
        return soilSiteInfoMapper.deleteSoilSiteInfoByIds(ids);
    }

    /**
     * 删除处置场所信息信息
     * 
     * @param id 处置场所信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSoilSiteInfoById(String id)
    {
        return soilSiteInfoMapper.deleteSoilSiteInfoById(id);
    }
}
